/*
Dominique Jannone
Guillaume Marre
Yohan Ojeda
Laurent Schiavon
*/
package Econf_Audio;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Logger;
import java.util.logging.Level;
 
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;

import model.eConf;
 
public class Receveur_client extends Thread{
  
		/**
           * @param args
           */
	// Attributs
    private static int portR;
	private static String loClient;
	public static Player player;
	//log
			public static final Logger logger = Logger.getLogger(Receveur_client.class.getName());
	
	// Constructeur
	public Receveur_client(int portR) {
		this.portR = portR;
		loClient = "127.0.0.1";
	}
	

	// Methode init
          public void run() {
                  String url= "rtp://"+loClient+":"+portR+"/audio/1";
                   
                  MediaLocator mrl= new MediaLocator(url);
                  
                  if (mrl == null) {
                	  logger.log(Level.SEVERE,"Can't build MRL for RTP");
                          System.exit(-1);
                  }
                  
                  // Create a player for this rtp session
                  player = null;
                  try {
                          player = Manager.createPlayer(mrl);
                  } catch (NoPlayerException e) {
                	  logger.log(Level.SEVERE,e.getClass() + ": " +  e.getMessage(), e);
                          System.exit(-1);
                  } catch (MalformedURLException e) {
                	  logger.log(Level.SEVERE,e.getClass() + ": " +  e.getMessage(), e);
                          System.exit(-1);
                  } catch (IOException e) {
                	  logger.log(Level.SEVERE,e.getClass() + ": " +  e.getMessage(), e);
                          System.exit(-1);
                 }
                  
                 if (player != null) {
                	 logger.log(Level.INFO,"Player created.");
                        player.realize();
 //               wait for realizing
                          while (player.getState() != Player.Realized){
                                  try {
                                          Thread.sleep(10);
                                } catch (InterruptedException e) {
                                	logger.log(Level.SEVERE,e.getClass() + ": " +  e.getMessage(), e);
                                  }
                          }
                          player.start();
                  } else {
                	  logger.log(Level.SEVERE,"Player doesn't created.");
                          System.exit(-1);
                  }
          }
  
}
